package com.mby.controller;

import com.mby.entity.CommonPics;
import com.mby.entity.CommonWords;
import com.mby.entity.PicsGroup;
import com.mby.entity.WordsGroup;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by noah on 2018/4/18.
 */
@RestController
public class CommonPicsController extends BaseController{

    /**
     * 创建一个常用图片
     * post /live/common_pics
     */
    @RequiresRoles(value={"10061025","10061017"},logical = Logical.OR)
    @PostMapping(value = "/common_pics", produces = "application/json; charset=UTF-8")
    public void create_common_pics(@RequestBody CommonPics commonPic){
        long count = mongoTool.count("{'id':"+commonPic.getGroupId()+"}",PicsGroup.class);
        if(count > 0){
            Integer group_id = commonPic.getGroupId();
            String[] urls = commonPic.getUrls();
            if(urls.length > 0){
                String url;
                for(int i=0;i<urls.length;i++){
                    commonPic = new CommonPics();
                    url = urls[i];
                    commonPic.setUrl(url);
                    commonPic.setGroupId(group_id);
                    commonPic.setDealerId(currentUser().getDealerId());
                    mongoTool.addOne(commonPic);
                }
            }
            respData(ok);
        }else{
            respErr("Nonexistent Group");
        }
    }

    /**
     * 修改一个常用图片
     * put /live/common_pics/{id}
     * @param id
     * @param commonPics(url,group_id)
     */
    @RequiresRoles(value={"10061025","10061017"},logical = Logical.OR)
    @PutMapping(value = "/common_pics/{id}", produces = "application/json; charset=UTF-8")
    public void update_common_pics(@PathVariable Integer id,@RequestBody CommonPics commonPics){
        mongoTool.update(commonPics,"{'id':"+id+",'dealerId':'"+currentUser().getDealerId()+"'}",CommonPics.class);
        respData(ok);
    }

    /**
     * 删除一个常用图片
     * delete /live/common_pics
     */
    @RequiresRoles(value={"10061025","10061017"},logical = Logical.OR)
    @DeleteMapping(value = "/common_pics")
    public void delete_common_pics(@RequestBody CommonPics commonPics){
        Integer[] ids = commonPics.getIds();
        if(ids.length > 0) {
            Integer id;
            for (int i = 0; i < ids.length; i++) {
                id = ids[i];
                mongoTool.update("{'deletedAt':'" + getNow() + "','deleted':true}", "{'id':" + id + ",'dealerId':'"+currentUser().getDealerId()+"'}", CommonPics.class);
            }
        }
        respData(ok);
    }

    /**
     * 查找当前用户的所有有效(未删除的)常用图片
     * get /live/common_pics?group_id=1&page_no=1&page_size=10
     */
    @GetMapping(value = "/common_pics")
    public void get_common_pics(Integer group_id,Integer page_no,Integer page_size){
        CommonPics cp = new CommonPics();
        cp.setGroupId(group_id);
        cp.setDealerId(currentUser().getDealerId());
        cp.setDeleted(false);
        List<?> list = mongoTool.findMany(cp,"id",CommonPics.class);
        List<CommonPics> result = new ArrayList<>();
        if(list != null && page_no != null && page_size != null){
            page_no = page_no -1;
            int i = page_no*page_size;
            while(i>=page_no*page_size && i<(page_no+1)*page_size && i<list.size()){
                cp = (CommonPics) list.get(i);
                result.add(cp);
                i++;
            }
            resultMap.put("total",list.size());
            resultMap.put("list",result);
            respData(resultMap);
        }else{
            respData(list);
        }
    }

    /**
     * 创建一个常用图片分类
     * post /live/pics_groups
     * @param picsGroup(name)
     */
    @RequiresRoles(value={"10061025","10061017"},logical = Logical.OR)
    @PostMapping(value = "/pics_groups", produces = "application/json; charset=UTF-8")
    public void create_pics_group(@RequestBody PicsGroup picsGroup){
        picsGroup.setDealerId(currentUser().getDealerId());
        respData(mongoTool.addOne(picsGroup));
    }

    /**
     * 修改一个常用图片分类
     * put /live/pics_groups/{id}
     * @param id
     * @param picsGroup(name)
     */
    @RequiresRoles(value={"10061025","10061017"},logical = Logical.OR)
    @PutMapping(value = "/pics_groups/{id}", produces = "application/json; charset=UTF-8")
    public void update_pics_group(@PathVariable Integer id,@RequestBody PicsGroup picsGroup){
        mongoTool.update(picsGroup,"{'id':"+id+",'dealerId':'"+currentUser().getDealerId()+"'}",PicsGroup.class);
        respData(ok);
    }

    /**
     * 删除一个常用图片分类
     * delete /live/pics_groups/{:id}
     * @param id
     */
    @RequiresRoles(value={"10061025","10061017"},logical = Logical.OR)
    @DeleteMapping(value = "/pics_groups/{id}")
    public void delete_pics_group(@PathVariable Integer id){
        mongoTool.update("{'deletedAt':'"+getNow()+"','deleted':true}","{'id':"+id+",'dealerId':'"+currentUser().getDealerId()+"'}",PicsGroup.class);
        respData(ok);
    }

    /**
     * 查找当前用户的所有有效(未删除的)常用图片分组
     * get /live/words_groups
     */
    @GetMapping(value = "/pics_groups")
    public void get_pics_groups(){
        respData(mongoTool.findMany("{'dealerId':'"+currentUser().getDealerId()+"','deleted':false}","name",PicsGroup.class));
    }

}
